package com.movitech.mobile.cz.base.util;

import com.alibaba.fastjson.JSONObject;
import com.movitech.mobile.cz.base.common.spring.SpringContextHolder;
import com.movitech.mobile.cz.base.common.util.HeadersUtils;
import com.movitech.mobile.cz.base.common.utils.JwtUtils;
import com.movitech.mobile.cz.base.sessionuser.*;
import com.movitech.mobile.cz.base.sys.bizvo.UserResponseVO;
import com.movitech.mobile.cz.base.sys.entity.ext.ExtSysUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * @author Jack.Gong
 * @since 2018-02-11
 *
 */

public class UserUtils {

//	public static String getCurrentUserId() {
//		return "1";
//	}

	public static ExtSysUser getCurrentUser() {
		ExtSysUser user = new ExtSysUser();
		user.setId("1");
		user.setRegionalCompanyId("1");
		user.setRegionalCompanyName("区域公司");
		return user;
	}

//	public static SessionUserVo getCurrentUserVo() {
//		SessionUserVo userVo = null;
//		try {
//			SessionUserService sessionUserService = (SessionUserService)SpringContextHolder.getBean("sessionUserService");
//			userVo = sessionUserService.getSessionUser();
//		} catch (Exception e) {
//		}
//		return userVo;
//	}
//
//
//	public static String getCurrentUserId() {
//		String userId = "";
//		SessionUserVo userVo = getCurrentUserVo();
//		if(userVo!=null) {
//			userId = userVo.getEmployeeID()+"";
//		}
//		return userId;
//	}

	public static SessionUserVo getCurrentUserVo() {
		SessionUserVo userVo = null;
		try {
			SessionUserService sessionUserService = (SessionUserService)SpringContextHolder.getBean("sessionUserService");
			ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
			HttpServletRequest request = requestAttributes.getRequest();
//			userVo = sessionUserService.getSessionUserByToken(HeadersUtils.getHeadByKey(request, "token"));
			userVo = sessionUserService.getSessionUserByToken(request);
		} catch (Exception e) {
		}
		return userVo;
	}
	public static String getRequestHeaderObjectBy(String key) {
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();
		return HeadersUtils.getHeadByKey(request, key);
	}

	public static String getRequestParameter(String key) {
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = requestAttributes.getRequest();
		return request.getParameter(key);
	}


	public static String getCurrentUserId() {
		String userId = "";
		SessionUserVo userVo = getCurrentUserVo();
		if(userVo!=null) {
			userId = userVo.getEmployeeID()+"";
		}
		return userId;
	}
	public static String getCurrentUserLoginName() {
		String userName = "";
		SessionUserVo userVo = getCurrentUserVo();
		if(userVo!=null) {
			userName = userVo.getUserName();
		}
		return userName;
	}

	public static List<SessionMenuVo> getCurrentUserMenu() {
		List<SessionMenuVo> lstSessionMenuVo = null;
		try {
			SessionMenuService sessionMenuService = (SessionMenuService)SpringContextHolder.getBean("sessionMenuService");
			lstSessionMenuVo = sessionMenuService.getSessionMenu();
		} catch (Exception e) {
		}
		return lstSessionMenuVo;
	}

	public static List<SessionButtonVo> getCurrentUserButton() {
		List<SessionButtonVo> lstSessionButtonVo = null;
		try {
			SessionButtonService sessionButtonService = (SessionButtonService)SpringContextHolder.getBean("sessionButtonService");
			lstSessionButtonVo = sessionButtonService.getSessionButton();
		} catch (Exception e) {
		}
		return lstSessionButtonVo;
	}

	public static String getOuterCurrentUserId() {
		String userId="";//"9261b5c898bf4c78b9099659fb74f3b1";
		try {
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		String userResponseVOJson= JwtUtils.getSubject(request);
			JSONObject obj=JSONObject.parseObject(userResponseVOJson);
			UserResponseVO jb = (UserResponseVO) JSONObject.toJavaObject(obj,UserResponseVO.class);
			userId=jb.getUserId();
		} catch (Exception e) {
		}
		return userId;
	}

	public static UserResponseVO getOuterCurrentUserVO() {
		UserResponseVO jb=null;
		try {
			HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
			String userResponseVOJson= JwtUtils.getSubject(request);
			JSONObject obj=JSONObject.parseObject(userResponseVOJson);
			jb = (UserResponseVO)JSONObject.toJavaObject(obj,UserResponseVO.class);
		} catch (Exception e) {

		}
//		jb= new UserResponseVO();
//		jb.setSuppliersId("282a943500d24ccf88d20ab0e5759399");
//		jb.setUserId("9261b5c898bf4c78b9099659fb74f3b1");
//		jb.setCompanyName("ellen");
		return jb;
	}

	public static String randomPassword() {
		String a = "0123456789";
		char[] rands = new char[5];
		for (int i = 0; i < rands.length; i++)
		{
			int rand = (int) (Math.random() * a.length());
			rands[i] = a.charAt(rand);
		}
		StringBuffer sb=new  StringBuffer();
		for(int i=0;i<rands.length;i++){
			sb.append(rands[i]);
		}
		return "z"+sb;
	}
}
